#include "mybutton.h"
#include<QPushButton>
#include<QDebug>
#include<QPixmap>
#include<QPropertyAnimation>
mybutton::mybutton(QString nimg, QString pimg)
{
    this->nimg=nimg;
    this->pimg=pimg;
    QPixmap pix;
    bool ret=pix.load(this->nimg);
    if(!ret)
    {
        qDebug()<<"图片加载失败";
    }
    this->setFixedSize(pix.width(),pix.height());

    this->setStyleSheet("QPushButton{border:0px;}");

    this->setIcon(pix);

    this->setIconSize(QSize(pix.width(),pix.height()));;
}

void mybutton::zoom1()
{
    //创建动画对象
    QPropertyAnimation *animation=new QPropertyAnimation(this,"geometry");

    //设置时间间隔
    animation->setDuration(200);
    //设置起始结束位置
    animation->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
    animation->setEndValue(QRect(this->x(),this->y()+10,this->width(),this->height()));

    animation->setEasingCurve(QEasingCurve::OutBounce);
    animation->start(QAbstractAnimation::DeleteWhenStopped);
}

void mybutton::zoom2()
{
    //创建动画对象
    QPropertyAnimation *animation=new QPropertyAnimation(this,"geometry");

    //设置时间间隔
    animation->setDuration(200);
    //设置起始结束位置
    animation->setStartValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    animation->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));

    animation->setEasingCurve(QEasingCurve::OutBounce);
    animation->start(QAbstractAnimation::DeleteWhenStopped);
}

void mybutton::mousePressEvent(QMouseEvent *e)
{
    if(this->pimg!=" ")
    {
        QPixmap pix;
        bool ret=pix.load(this->pimg);
        if(!ret)
        {
            qDebug()<<"图片加载失败";
        }
        this->setFixedSize(pix.width(),pix.height());

        this->setStyleSheet("QPushButton{border:0px;}");

        this->setIcon(pix);

        this->setIconSize(QSize(pix.width(),pix.height()));;
    }
    //其他的事情交给父类处理
    QPushButton::mousePressEvent(e);
}

void mybutton::mouseReleaseEvent(QMouseEvent *e)
{
    if(this->pimg!=" ")
    {
        QPixmap pix;
        bool ret=pix.load(this->nimg);
        if(!ret)
        {
            qDebug()<<"图片加载失败";
        }
        this->setFixedSize(pix.width(),pix.height());

        this->setStyleSheet("QPushButton{border:0px;}");

        this->setIcon(pix);

        this->setIconSize(QSize(pix.width(),pix.height()));;
    }
    //其他的事情交给父类处理
    QPushButton::mouseReleaseEvent(e);
}
